package Servlet;

import JavaBeans.User;
import com.mysql.jdbc.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Brian
 */
public class order extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(registration.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(registration.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(registration.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(registration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        String stock = (String)request.getParameter("stock");
        String []temp = stock.split(" "); 
        String stockname = temp[0];
        float price = Float.parseFloat(temp[1]);
        
        HttpSession ss = request.getSession(true);
        User user = (User)ss.getAttribute("user");

        Connection con = (Connection) dataBaseController.getConnection();
        Statement st = null;
        ResultSet rs = null;

        st = con.createStatement();
        st = (Statement) con.createStatement();

        try{
        st.executeUpdate("INSERT INTO `brchung`.`order1` (`NumShares`, `PricePerShare`, `DateTime`, `Percentage`, `Price`, `Order1`) VALUES ("
                + request.getParameter("share") +", "+price+", NOW(), '"+request.getParameter("percentage") +"', '"+ request.getParameter("price") +"', '"+request.getParameter("order") +"');");
        rs = (ResultSet) st.executeQuery("SELECT MAX(id) as id FROM brchung.order1;");
        rs.next();
        String oid = rs.getString(1);
        
        
        st.executeUpdate("INSERT INTO `brchung`.`transaction` (`Fee`, `DateTime`, `PricePerShare`) VALUES (10, NOW(), "+price+");");
        rs = (ResultSet) st.executeQuery("SELECT MAX(id) as id FROM brchung.transaction;");
        rs.next();
        String tid = rs.getString(1);
        
        st.executeUpdate("INSERT INTO `brchung`.`trade` (`AccountId`, `BrokerId`, `TransactionId`, `OrderId`, `StockId`) "
                + "VALUES ("+request.getParameter("acc")+", "+user.getSsn()+", "+tid+", "+ oid +", '"+ stockname +"');");
        }
        catch(Exception e)
        {
        }
        request.setAttribute("order","abc");
        request.getRequestDispatcher("index.jsp").forward(request, response);
                return;

    }
}